<?php
/**
 * Created by JetBrains PhpStorm.
 * User: akrug
 * Date: 29.05.12
 * Time: 17:13
 * To change this template use File | Settings | File Templates.
 */
class Backend_Model_DbTable_Images extends Zend_Db_Table_Abstract
{

    protected $_name = 'img_images';

    public function init(){
        $this->_setupPrimaryKey();//identifies primerykey
    }

    /**
     * @return array
     * @purpose returns all undeleted companies
     */
    public function getImageList(){
        $select = $this->select();
                        //->where('deleted = 0');
        $result = $this->fetchAll($select)->toArray();

        return $result;
    }

    /**
     * @return array - one defined Company
     */
    public function getCompany($id){
        if(!self::deleted($id)){
            $select = $this->select()->where( $this->_primary[1].' = '.$id);
            $result = $this->fetchAll($select)->toArray();
            return $result[0];
        }

        return false;
    }

    /**
     * @param $id
     * @return bool - returns true if 'deleted'  = 0
     *              - returns false if 'deleted' = 1 OR id not found
     */
    public function deleted($id){
        $select = $this->select()
                        ->where( $this->_primary[1].' = '.$id);

        $result = $this->fetchAll($select)->toArray();

        if(empty($result)){
            return true;
        }elseif($result[0]['deleted'] == '1'){
            return true;
        }

        return false;
    }

    /**
     * @param $id
     * @return bool
     *
     * @purpose sets deleted flag to 1
     */
    public function deleteCompany($id){

        if(!self::deleted($id)){
            $rowsDeleted = $this->update(array('deleted' => 1),  $this->_primary[1].' = '.$id);

            if(0 == $rowsDeleted){
                return false;
            }
        }

        return true;
    }

    /**
     * @param $params
     * @return bool
     *
     * @purpose compares each given param index with columnnames
     *          if there is no match param will be unset
     */
    public function editCompany($params){
        $cols    = $this->_getCols();
        $id      = $params['id'];

        if(!$this->deleted($id)){
            foreach($params as $pIndex => $p){
                if(!in_array($pIndex, $cols)){
                    unset($params[$pIndex]);
                }
            }
            $this->update($params,  $this->_primary[1].' = '.$id);

            return true;
        }

        return false;
    }

    /**
     * @param $id
     * @return bool
     *
     * @purpose locks if locked - unlocks if unlocked
     *
     */
    public function lockCompany($id){

        if(!self::deleted($id)){
            $hidden = 1;

            if($this->locked($id)){
                $hidden = 0;
            }

            $rowsDeleted = $this->update(array('hidden' => $hidden),  $this->_primary[1].' = '.$id);

            if(0 == $rowsDeleted){
                return false;
            }
        }

        return true;
    }

    /**
     * @param $id
     * @return bool
     *
     * @purpose checks if company is locked
     */
    public function locked($id){
        $company = $this->getCompany($id);

        return (bool) $company['hidden'];
    }
}
